package com.zj.healthmanage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.pojo.Checkitem;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zj.healthmanage.mapper.MemberMapper;
import com.zj.healthmanage.pojo.Member;
import com.zj.healthmanage.service.MemberService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService{

    @Resource
    private MemberMapper memberMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Member record) {
        return memberMapper.insert(record);
    }

    @Override
    public int insertSelective(Member record) {
        return memberMapper.insertSelective(record);
    }

    @Override
    public Member selectByPrimaryKey(Integer id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Member record) {
        return memberMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Member record) {
        return memberMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryMemberByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberMapper.queryMemberByPage(queryString);
        long total = page.getTotal();
        List<Member> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public Member queryMemberById(Integer id) {
        return memberMapper.queryMemberById(id);
    }

    @Override
    public List<Map<String, Object>> exportMemberInfo(Integer[] memberIdList) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Integer memberId : memberIdList) {
            Map<String, Object> map = new HashMap<>();
            //获取每个会员的信息
            Member member = memberMapper.queryMemberById(memberId);
            String name = member.getName();
            String sex = member.getSex();
            String idCard = member.getIdcard();
            String phoneNumber = member.getPhonenumber();
            String username = member.getUsername();
            String password = member.getPassword();
            String email = member.getEmail();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String birthday = dateFormat.format(member.getBirthday());
            String maritalStatus = member.getMaritalstatus();
            String profession = member.getProfession();
            String educationalLevel = member.getEducationallevel();
            String medicalHistory = member.getMedicalhistory();
            String geneticHistory = member.getGenetichistory();
            String surgicalHistory = member.getSurgicalhistory();
            DateFormat Format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String regTime = dateFormat.format(member.getRegtime());
            String modifyTime = dateFormat.format(member.getModifytime());
            map.put("name", name);
            map.put("sex", sex);
            map.put("idCard", idCard);
            map.put("phoneNumber", phoneNumber);
            map.put("username", username);
            map.put("password", password);
            map.put("email", email);
            map.put("birthday", birthday);
            map.put("maritalStatus", maritalStatus);
            map.put("profession", profession);
            map.put("educationalLevel", educationalLevel);
            map.put("medicalHistory", medicalHistory);
            map.put("geneticHistory", geneticHistory);
            map.put("surgicalHistory", surgicalHistory);
            map.put("regTime", regTime);
            map.put("modifyTime", modifyTime);
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<Integer> queryMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberMapper.queryMemberCountByMonth(m);
            list.add(count);
        }
        return list;
    }

}
